<?php
include "../lib/DAL/DmChucVuLop.php";

class DmChucVuLopBLL{
    function __construct(){
    }
    
    function __destruct(){
    }

    public function SetChucVuLopBLL($row)
    {
        $chucvulop= new DmChucVuLop();
        $chucvulop->setchucvulop($row['madm_chucvulop'],$row['ten_chucvulop']);
        return $chucvulop;   
    }
    
    public function GetlistChucVuLop(){
        $ex=mysql_query("SELECT * FROM dm_chucvulop");
        $ds= array();
        if($ex){
            while($row=mysql_fetch_array($ex)){
                $ds[]=$this->SetChucVuLopBLL($row);
            }
        }
     return $ds;
    }
    
    public function getListChucVuLopByID($id){
            $sql= " SELECT * FROM dm_chucvulop WHERE madm_chucvulop= '".$id."'";
            $result = mysql_query($sql) or die(mysql_error());
            $arrcvl=array();
            while($row = mysql_fetch_array($result)){
                $arrcvl[]=$this->SetChucVuLopBLL($row);
            }
            return $arrcvl[0];
        }

    public function InsertChucvulop($dt)
    {
        $sql="  INSERT INTO dm_chucvulop(madm_chucvulop,ten_chucvulop) 
                VALUES('".$dt->getmadm_chucvulop()."','".$dt->getten_chucvulop()."')";
        
        $kq= mysql_query($sql);
        
        return $kq;
    }
    
    public function UpdateChucvulop($madm_chucvulop,$ten_chucvulop){
        $sql="  UPDATE dm_chucvulop 
        SET
            ten_chucvulop = '".$ten_chucvulop."' 
           
            WHERE madm_chucvulop ='".$madm_chucvulop."'";
          $result = mysql_query($sql) or die (mysql_error());
       return $result;
       
             
    }
    
    public function DelChucvulop($id){
            $sql="  DELETE FROM dm_chucvulop WHERE madm_chucvulop='".$id."'";
            $kq = mysql_query($sql) or die(mysql_error());
            return $kq;
    }
public function countChucvulop(){
        $socvl=mysql_query("select madm_chucvulop from dm_chucvulop");
        
        return mysql_num_rows($socvl); 
    }   
//Ph?n Thêm vào
    public function getItemChucvulop($madm_chucvulop){
        $sql="select * from dm_chucvulop where madm_chucvulop='".$madm_chucvulop."'";
        $ex=mysql_query($sql);
        if (mysql_num_rows($ex)){
            $kq =mysql_fetch_array($ex);
            }
        return $kq;
    }
//Phân trang
    public function getPages($offset,$limit){
      $result = mysql_query("SELECT * 
                            FROM dm_chucvulop 
                            LIMIT $offset, $limit");
      $arr_cvl= array();
      
      while($row = mysql_fetch_array($result)){
        $arr_cvl[] =  $this->SetChucVuLopBLL($row);//Co the sai
      }
      return $arr_cvl;
    }
    function paging($totalRecords,$limit,$url,$css){
		$totalPage=ceil($totalRecords/$limit);
		$pager='------>';
    
		for($i=1;$i<$totalPage;$i++){
		  $pager.="<a href='$url&p=$i'.'\' class='$css'>".$i."|</a>";
          }
          if($i=$totalPage)
            $pager.="<a href='$url&p=$i' class='$css'>Trang cuối</a>";
		
		return $pager;
	}
    
//// tìm ki?m /////
//Lo?i b? d?u ti?ng vi?t
    function loaiDauTV($str) {
		$chars = array(
			'a'		=>    array('?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','á','à','?','ã','?','â','a','Á','À','?','Ã','?','Â','A'),
			'e'		=>    array('?','?','?','?','?','?','?','?','?','?','é','è','?','?','?','ê','É','È','?','?','?','Ê'),
			'i'		=>    array('í','ì','?','i','?','Í','Ì','?','I','?'),
			'o'		=>    array('?','?','?','?','?','?','?','?','Ô','?','?','?','?','?','?','?','?','?','?','?','ó','ò','?','õ','?','ô','o','Ó','Ò','?','Õ','?','Ô','O'),
			'u'		=>    array('?','?','?','?','?','?','?','?','?','?','ú','ù','?','u','?','u','Ú','Ù','?','U','?','U'),
			'y'		=>    array('ý','?','?','?','?','Ý','?','?','?','?'),
			'd'		=>    array('d','Ð'),
		);
	foreach ($chars as $key => $arr) 
		foreach ($arr as $val)
			$str = str_replace($val,$key,$str);
		$str = strtolower($str);
		
		return $str;
	}
    
	function tachChuoi($str){
		$str = $this->loaiDauTV($str);	
		$symbolsRemove = array('/','\\','\'','"',',','.','<','>','?',';',':','[',']','{','}','|','=','+','-','_',')','(','*','&','^','%','$','#','@','!','~','`');
        for ($i = 0; $i < sizeof($symbolsRemove); $i++){
			$str = str_replace($symbolsRemove[$i],' ',$str);
		}
		
		//Xoá kho?ng tr?ng (>=2), tab, xu?ng dòng
		$str = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $str);
		
		 //Tách chu?i thành m?ng b?i d?u cách
		$arrKw = explode(' ',$str);
		//$arrKw = split(' ',$str);
		 //T?o m?ng k?t qu? ch?a keyword
		$kwResult = array();
		
		//Duy?t m?ng và dua vào các t? khoá h?p l?
		foreach($arrKw as $arr){
			if($arr!=''){
				array_push($kwResult,$arr);
			}
			}
			
			return $kwResult;
		} 
	function buildSearchQueryLike($keyword){    
		$dieukien="";   
		$qse=" OR ";
		$arr = $this->tachChuoi($keyword);	
		foreach($arr as $kw){						
			$dieukien .=" madm_chucvulop LIKE '%$kw%' OR ten_chucvulop LIKE '%$kw%'";			
			$dieukien .= $qse;	
							
		}
		$cstr = strlen($qse);
		$dieukien = substr($dieukien,0,-$cstr);		
		return $dieukien;	
		
	}
    function timkiem($where){
        $sql="SELECT madm_chucvulop, ten_chucvulop FROM dm_chucvulop $where";
        $ex=mysql_query($sql);
        if($ex){
            while($row=mysql_fetch_array($ex)){
                $dstk[]=$this->SetChucVuLopBLL($row);///co the sai
            }
        }
        return $dstk;
    }
//xoa tat ca
    public function deleteAll(){
    $sql="DELETE FROM dm_chucvulop";
    $result = mysql_query($sql) or die (mysql_error());
    return $result;
}
}
?>